<?php
/**
 * Created by PhpStorm.
 * User: sx-2
 * Date: 2016/9/8
 * Time: 10:17
 */
class cats_mdl extends Model{

    public static $table = 'mac_tm_cats';

    //获得所有分类
    public function get_list($order = "cid"){
        $sql = 'SELECT *
				FROM '.self::$table.'
				ORDER BY '.$order ;

        $rs = $this->dbh->get_all($sql);
        return $rs;
    }


    //保存
    public function add_cat($cid, $name, $parent_cid, $pic_url){

        $item = $this->get_one($cid);
        if(empty($item['cid'])){
            $this->dbh->insert(self::$table,
                array(
                    'cid' => $cid,
                    'name' => $name,
                    'parent_cid' => $parent_cid,
                    'pic_url' => $pic_url
                )
            );
            $rs_id = $this->dbh->insert_id();
        }else {
            $this->dbh->update(self::$table,
                array(
                    'name' => $name,
                    'parent_cid' => $parent_cid,
                    'pic_url' => $pic_url
                ),
                'cid=' . $cid
            );
            $item = $this->get_one($cid);
        }

        return $item;
    }

    /*public function add_cat($id, $parent_id, $name, $cid, $props, $sku_props, $type, $hidden){

        $item = $this->get_one($id);
        if(empty($item['id'])){
            $this->dbh->insert(self::$table,
                array(
                    'parent_id' => $parent_id,
                    'name' => $name,
                    'cid' => $cid,
                    'props' => $props,
                    'sku_props' => $sku_props,
                    'type' => $type,
                    'hidden' => $hidden
                )
            );
            $rs_id = $this->dbh->insert_id();
        }else {
            $this->dbh->update(self::$table,
                array(
                    'parent_id' => $parent_id,
                    'name' => $name,
                    'cid' => $cid,
                    'props' => $props,
                    'sku_props' => $sku_props,
                    'type' => $type,
                    'hidden' => $hidden
                ),
                'id=' . $id
            );
            $item = $this->get_one($id);
        }

        return $item;
    }*/

    //获得所有分类
    public function get_one($cid){
        $rs = '';
        if(!empty($cid)) {
            $sql = 'SELECT *
				FROM ' . self::$table . '
				WHERE cid = ' . $cid;

            $rs = $this->dbh->get_one($sql);
        }
        return $rs;
    }


    //查所有商品ID
    public function get_cats_id()
    {
        $sql = "SELECT COUNT(1) count FROM " .self::$table;
        $count = $this->dbh->get_one($sql);
        $count = $count["count"];

        //$pageno = 1;
        $pagesize = 200;

        $ids = array();

        $pages = ceil($count / $pagesize);

        for($pageno = 1;$pageno <= $pages; $pageno++){
            $sql = 'SELECT cid
                FROM '.self::$table.'
                LIMIT '.(($pageno-1)*$pagesize).' , '.$pagesize;

            $rs = $this->dbh->get_all($sql);
            array_push($ids, array_column($rs, 'cid'));
        }

        return array(
            "total"=>$count,
            "list"=>$ids[0]
        );
    }


    //删除不存在的分类
    public function del_cats_ids($del_ids)
    {
        $result = '';
        foreach ($del_ids as $id){
            $result = $this->dbh->delete(self::$table," cid = ".$id);
        }
        return $result;
    }
}